import 'package:flutter/material.dart';
import 'package:kiomax/config/string_config.dart';
import 'package:kiomax/config/color_config.dart';
import 'package:kiomax/pages/install/install_reader_page.dart';
import '../../pages/login/login_page.dart';
import '../../utils/common_util.dart';

class AppDrawer extends StatelessWidget {
  final String locationId;

  const AppDrawer({
    Key? key,
    required this.locationId,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      backgroundColor: Colors.white,
      child: Column(
        children: [
          const SizedBox(height: 48),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 24),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  StringConfig.kiomax_installer,
                  style: TextStyle(
                    color: ColorConfig.drawerTextPrimary,
                    fontSize: 20,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.location_on_outlined,
                      size: 16,
                      color: ColorConfig.drawerIconColor,
                    ),
                    const SizedBox(width: 4),
                    Text(
                      '${StringConfig.location_id}$locationId',
                      style: TextStyle(
                        color: ColorConfig.drawerTextSecondary,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          const SizedBox(height: 32),
          _DrawerItem(
            icon: Icons.home_outlined,
            label: StringConfig.menu_home,
            onTap: () {
              Navigator.pop(context);
            },
          ),
          _DrawerItem(
            icon: Icons.qr_code_scanner_outlined,
            label: StringConfig.menu_install_reader,
            onTap: () {
              Navigator.pop(context);
              Navigator.push(context, MaterialPageRoute( builder: (dcontext) => const InstallReaderPage(),
              ));
            },
          ),
          _DrawerItem(
            icon: Icons.build_outlined,
            label: StringConfig.menu_service,
            onTap: () {
              Navigator.pop(context);
            },
          ),
          _DrawerItem(
            icon: Icons.account_balance_wallet_outlined,
            label: StringConfig.menu_cash_collection,
            onTap: () {
              Navigator.pop(context);
            },
          ),
          const Spacer(),
          _DrawerItem(
            icon: Icons.settings_outlined,
            label: StringConfig.menu_settings,
            onTap: () {
              Navigator.pop(context);
            },
          ),
          _DrawerItem(
            icon: Icons.logout_outlined,
            label: StringConfig.menu_sign_out,
            onTap: () {
              CommonUtil.showTipDialog(
                context,
                title: StringConfig.sign_out_confirmation,
                message: StringConfig.sign_out_confirmation_tip,
                onCancel: ()=>Navigator.pop(context),
                onConfirm: () {
                  Navigator.of(context).pushAndRemoveUntil(
                    MaterialPageRoute(builder: (diacontext) => const LoginPage()),
                    (route) => false,
                  );
                },
              );
            },
          ),
          const SizedBox(height: 16),
        ],
      ),
    );
  }
}

class _DrawerItem extends StatelessWidget {
  final IconData icon;
  final String label;
  final VoidCallback onTap;

  const _DrawerItem({
    Key? key,
    required this.icon,
    required this.label,
    required this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
        child: Row(
          children: [
            Icon(
              icon,
              size: 24,
              color: ColorConfig.drawerIconColor,
            ),
            const SizedBox(width: 12),
            Text(
              label,
              style: TextStyle(
                color: ColorConfig.drawerTextPrimary,
                fontSize: 16,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
